const nameInp=document.querySelector('.username')
const pwdInp=document.querySelector('.password')
const nickInp = document.querySelector('.nickname')
const err=document.querySelector('.error')
const rpwdInp = document.querySelector('.rpassword')

const form=document.querySelector('form')
form.addEventListener('submit',e=>{
    e=e||window.event
    e.preventDefault()
    const name =nameInp.value
    const pwd=pwdInp.value
    const rpwd=rpwdInp.value
    const nick = nickInp.value

    if(!name || !pwd || !rpwd||!nick) return alert('请完整填写表单')
    if(pwd!==rpwd){
        err.innerText='密码不一致，请再次输入'
        err.style.visibility='visible'
        return
    }
    const data=`username=${name}&password=${pwd}&rpassword=${rpwd}&nickname=${nick}`
    ajax({
        url:'http://localhost:8888/users/register',
        data,
        method:'POST',
        dataType:'json',
        success(res){
            if(res.code===0){
                err.innerText='该用户名已经存在, 请重试!'
                err.style.visibility='visible'
                return
                
            }console.log(res)
            if(res.code===1){
                alert('注册成功, 点击确定回登录页')
                window.location.href='./login.html'
            }
        }
    })
})